<?php
class VacanciesController extends AppController {

	var $name = 'Vacancies';
	var $helpers = array('Html', 'Form');
	var $uses = array('Vacancy','Store','Apply', 'Invite');
	var $components = array('Session','RequestHandler');

	function beforeFilter()
	{
		parent::beforeFilter();
		$user = $this->Session->read('User.User');
		if( !$user )
		$this->redirect(array('controller'=>'errors','action' => 'permission'));
		
		// Get my invite
     	$a_Invite = $this->Invite->v_fGetInviteByPharmacyId( $user['id'] );
     	$this->set('a_PharInvite', $a_Invite);
     	
     	// Get i am invited
     	$a_Invite = $this->Invite->v_fGetInviteByLocumId( $user['id'] );
     	$this->set('a_StoreInvite', $a_Invite);
	}

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action' => 'index'));
		}

		$a_DetailVacancy = $this->Vacancy->getDetailVacancyByPharmacyIdAndVacancyId($id, $this->Session->read('User.User.id	'));

		if(!empty($a_DetailVacancy))
		{
			$a_Event = explode(',',$a_DetailVacancy['Vacancy']['DateRange']);
			$a_Temps = array();
			if($a_Event)
			{
				foreach($a_Event as $event)
				{
					$a_Temps[] = strtotime($event);
				}
			}
			sort($a_Temps);
			$i_currentMonth = date('n');
			// $a_EventMonth = explode('-',date('m'));
			$i_eventMonth = date('m',$a_Temps[0]);
			$jump = $i_eventMonth - $i_currentMonth;
			 
			$this->set('jump',$jump);
			$this->set('a_DetailVacancy', $a_DetailVacancy);
		}else
		{
			$this->redirect(array('controller'=>'errors','action' => 'error404'));
		}

	}
	
	function view_invite($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action' => 'index'));
		}

		$a_DetailVacancy = $this->Vacancy->getDetailVacancyByPharmacyIdAndVacancyId($id, $this->Session->read('User.User.id	'));

		if(!empty($a_DetailVacancy))
		{
			$a_Event = explode(',',$a_DetailVacancy['Vacancy']['DateRange']);
			$a_Temps = array();
			if($a_Event)
			{
				foreach($a_Event as $event)
				{
					$a_Temps[] = strtotime($event);
				}
			}
			sort($a_Temps);
			$i_currentMonth = date('n');
			// $a_EventMonth = explode('-',date('m'));
			$i_eventMonth = date('m',$a_Temps[0]);
			$jump = $i_eventMonth - $i_currentMonth;
			 
			$this->set('jump',$jump);
			$this->set('a_DetailVacancy', $a_DetailVacancy);
		}else
		{
			$this->redirect(array('controller'=>'errors','action' => 'error404'));
		}

	}

	function add() {

		$UserInfo = $this->Session->read('User');
		$this->set('UserInfo',$UserInfo);
		//List Store
		$a_ListStores = $this->Store->getListStoresByPharmacyId($UserInfo['User']['id']);
		$this->set('a_ListStores',$a_ListStores);
		if (!empty($this->data)) {

			$storeDetail = $this->Store->getStoresById($this->data['Vacancy']['Store_id']);
			$this->Vacancy->create();
			$this->Vacancy->set($this->data);
			if($this->Vacancy->ValidateData() == true){
				//build standard data
				$this->data['Vacancy']['avatar'] = $storeDetail[0]['Store']['Avatar'];
				 
				if($this->data['Vacancy']['event'])
				$this->data['Vacancy']['DateRange'] = $this->data['Vacancy']['event'];
				 
				$this->data['Vacancy']['Pharmacy_id'] = $UserInfo['User']['id'];
				$this->data['Vacancy']['DateCreated'] = date('Y-m-d H:i:s');
				// Get county id
				$a_Store = $this->Store->getStoresById($this->data['Vacancy']['Store_id']);

				$this->data['Vacancy']['County_id'] = $a_Store[0]['Store']['Country_id'];
				// Get PostCode
				$this->data['Vacancy']['PostCode'] = $a_Store[0]['Store']['Postcode'];
				 
				// print_r($this->data); return;
				unset($this->data['Vacancy']['event']);
				if ($this->Vacancy->save($this->data)) {
					$id=$this->Vacancy->id;
					$this->redirect(array('controller'=>'vacancies','action' => 'view/'.$id));
				} else {
					$this->Session->setFlash(__('The Store could not be saved. Please, try again.', true));
				}
			}
		}
	}

	function edit($id = null) {
		$UserInfo = $this->Session->read('User');
		//List Store
		$a_ListStores = $this->Store->getListStoresByPharmacyId($UserInfo['User']['id']);
		$this->set('a_ListStores',$a_ListStores);
		//  print_r($a_ListStores);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->Vacancy->set($this->data);

			if($this->Vacancy->ValidateData() == true){
				//build standard data
				if($this->data['Vacancy']['event'])
				$this->data['Vacancy']['DateRange'] = date('Y-m-d',strtotime($this->data['Vacancy']['event']));

				if ($this->Vacancy->save($this->data)) {
					$id=$this->Store->id;
					$this->Session->setFlash(__('The Store has been saved', true));
					$this->redirect(array('controller'=>'vacancies','action' => 'view/'.$id));
				} else {
					$this->Session->setFlash(__('The Store could not be saved. Please, try again.', true));
				}
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Vacancy->read(null, $id);
		}
	}

	function delete($id = null) {
		Configure::write('debug', 0);
		$this->layout =  null;
		$this->layout =  false;
		if( $id )
		{
			$a_DetailVacancy = $this->Vacancy->getDetailVacancyByPharmacyIdAndVacancyId($id, $this->Session->read('User.User.id	'));
			if( $a_DetailVacancy )
			{
				$this->Vacancy->delete($id);
				$a_Errors["success"]="true";
				echo json_encode($a_Errors);
				$this->render(false);
			}
		}
		$this->render(false);
		
	}

	function getJson($id = null)
	{
		Configure::write('debug', 0);
		$this->autoRender = false;
		$this->layout =  false;
		$sz_StartEvent ='[';
		$sz_EndEvent =']';
		$this->set('id',$id);
		$a_Events = $this->Apply->applideDateRage($id);
		if($a_Events)
		{
			$a_Event = explode(',',$a_Events['Apply']['available']);
			 
			$i=1;
			$n= count($a_Event);
			foreach($a_Event as $k=>$event){
				if($i < $n)
				$sz_StartEvent .= '{"date": '.'"'.date('Y-m-d',strtotime($event)).'"'.'},';
				else
				$sz_StartEvent .= '{"date": '.'"'.date('Y-m-d',strtotime($event)).'"'.'}';
				$i++;
			}
		}


		echo $sz_StartEvent.$sz_EndEvent;
		$this->render(false);
	}

	function getJson2($id = null)
	{
		Configure::write('debug', 0);
		$this->autoRender = false;
		$this->layout =  false;
		$sz_StartEvent ='[';
		$sz_EndEvent =']';
		$this->set('id',$id);
		$a_Events = $this->Vacancy->find('first',array('fields'=>'DateRange','conditions'=>array('id'=>$id)));
		if($a_Events)
		{
			$a_Event = explode(',',$a_Events['Vacancy']['DateRange']);
			 
			$i=1;
			$n= count($a_Event);
			foreach($a_Event as $k=>$event){
				if($i < $n)
				$sz_StartEvent .= '{"date": '.'"'.date('Y-m-d',strtotime($event)).'"'.'},';
				else
				$sz_StartEvent .= '{"date": '.'"'.date('Y-m-d',strtotime($event)).'"'.'}';
				$i++;
			}
		}


		echo $sz_StartEvent.$sz_EndEvent;
		$this->render(false);
	}
	
	function getJsonApply($id = null)
    {
        Configure::write('debug', 0);
        $this->autoRender = false;
        $this->layout =  false;
        $sz_StartEvent ='[';
        $sz_EndEvent =']';
        $this->set('id',$id);
        $a_Events = $this->Apply->applideDateRage($id); 
        if($a_Events)
        {
           $a_EventsVacancy = $this->Vacancy->find('first',array('fields'=>'DateRange','conditions'=>array('id'=>$a_Events['Apply']['vacancy_id']))); 
	        if($a_EventsVacancy)
	        {
	           $a_EventVacancy = explode(',',$a_EventsVacancy['Vacancy']['DateRange']);
	           $a_EventVacancy = array_unique($a_EventVacancy);
	           $i=1;
	           $n= count($a_EventVacancy);
	           foreach($a_EventVacancy as $k=>$e){
	              if($i < $n)
	                $sz_StartEvent .= '{"date": '.'"'.date('Y-m-d',strtotime($e)).'"'.'},';
	              else
	                 $sz_StartEvent .= '{"date": '.'"'.date('Y-m-d',strtotime($e)).'"'.'}';
	              $i++;
	           }
	        }
           
           
           $a_Event = explode(',',$a_Events['Apply']['available']);
           $a_Event = array_unique($a_Event);
           $i=1;
           $n= count($a_Event);
           foreach($a_Event as $k=>$event){
              if($i < $n)
                $sz_StartEvent .= ',{"date2": '.'"'.date('Y-m-d',strtotime($event)).'"'.'}';
              else
                 $sz_StartEvent .= ',{"date2": '.'"'.date('Y-m-d',strtotime($event)).'"'.'}';
              $i++;
           }
        }


        echo $sz_StartEvent.$sz_EndEvent;
        $this->render(false);
    }
}
?>